package cn.promonkey.api.menu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.promonkey.core.dto.UserDto;
import cn.promonkey.core.response.ResultMsg;
import cn.promonkey.core.toolkit.QueryGenerator;
import cn.promonkey.core.toolkit.UserInfoUtils;
import cn.promonkey.api.menu.entity.Menu;
import cn.promonkey.api.menu.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/** @author <no value> */
@Slf4j
@RestController
@RequestMapping("/menu/")
public class MenuController {
  final MenuService menuService;

  public MenuController(MenuService menuService) {
    this.menuService = menuService;
  }

  @RequestMapping("getPage")
  public String getPage(
      Menu menu,
      @RequestParam(name = "page", defaultValue = "1") Integer page,
      @RequestParam(name = "size", defaultValue = "10") Integer size,
      HttpServletRequest request) {
    QueryWrapper<Menu> queryWrapper = QueryGenerator.initQueryWrapper(menu, request.getParameterMap());
    Page<Menu> pageable = new Page<>(page, size);
    IPage<Menu> pageList = menuService.page(pageable, queryWrapper);
    return ResultMsg.success(pageList);
  }

  @RequestMapping(value = "save", method = RequestMethod.POST)
  public String save(@RequestBody Menu menu) {
	  
	  UserDto sysuser = UserInfoUtils.getUserInfo();
	  menu.setBusinessId(sysuser.getBusinessId());
    return ResultMsg.success(menuService.save(menu));
  }

  @RequestMapping(value = "edit", method = RequestMethod.POST)
  public String edit(@RequestBody Menu menu) {
    return ResultMsg.success(menuService.updateById(menu));
  }

  @RequestMapping(value = "get")
  public String get(@RequestParam(name = "id") Long id) {
    return ResultMsg.success(menuService.getById(id));
  }

  @RequestMapping(value = "delete", method = RequestMethod.POST)
  public String delete(@RequestBody Long id) {
    return ResultMsg.success(menuService.removeById(id));
  }
  
  @RequestMapping(value = "getTree")
  public String getTree(@RequestParam(name = "systemId") Long systemId) {
    return ResultMsg.success(menuService.getTreeList(systemId));
  }
}
